package com.example.houseman.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.houseman.entity.TbHouseInfo;
import com.example.houseman.entity.TbHouseStyle;
import com.example.houseman.entity.TbRelationHousePicture;
import com.example.houseman.entity.TbRelationRoomPicture;
import com.example.houseman.mapper.TbHouseInfoMapper;
import com.example.houseman.mapper.TbHouseStyleMapper;
import com.example.houseman.mapper.TbRelationHousePictureMapper;
import com.example.houseman.mapper.TbRelationRoomPictureMapper;
import com.example.houseman.service.ITbHouseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.org.dto.HouseInfoDto;
import com.org.dto.TbHouseInfoDto;
import com.org.dto.TbHouseStyleDto;
import com.org.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 龚梦
 * @since 2021-03-31
 */
@Service
@Slf4j
public class TbHouseInfoServiceImpl extends ServiceImpl<TbHouseInfoMapper, TbHouseInfo> implements ITbHouseInfoService {

    @Autowired
    private TbHouseInfoMapper tbHouseInfoMapper;

    @Autowired
    private TbRelationHousePictureMapper tbRelationHousePictureMapper;

    @Autowired
    private TbHouseStyleMapper tbHouseStyleMapper;

    @Autowired
    private TbRelationRoomPictureMapper relationRoomPictureMapper;

    @Override
    @Transactional
    public ResultEntity saveinfo(TbHouseInfoDto tbHouseInfoDto) {


        String gids = "";
        TbHouseInfo tbHouseInfo = new TbHouseInfo();
        BeanUtils.copyProperties(tbHouseInfoDto, tbHouseInfo);
        Integer[] ggids = tbHouseInfoDto.getGgids();
        for (int i = 0; i < ggids.length; i++) {
            gids += ggids[i];
            if (i < ggids.length) {
                gids += ",";
            }
        }
        tbHouseInfo.setPublicFacility(gids);
        tbHouseInfo.setCreateTime(new Date().toString());
        log.info(tbHouseInfo.toString() + "====公共设施=====");
        int insert = tbHouseInfoMapper.insert(tbHouseInfo);


        List<TbHouseStyleDto> tbHouseStyleDtos = tbHouseInfoDto.getTbHouseStyleDtos();
        for (TbHouseStyleDto houseStyleDto : tbHouseStyleDtos) {
            String ids = null;
            TbHouseStyle tbHouseStyle = new TbHouseStyle();
            BeanUtils.copyProperties(houseStyleDto, tbHouseStyle);
            Integer[] duiliids = houseStyleDto.getDuiliids();
            for (Integer duiliid : duiliids) {
                ids += "," + duiliid;
            }
            tbHouseStyle.setHouseId(tbHouseInfo.getId());
            tbHouseStyle.setPrivateFacility(ids);


            tbHouseStyleMapper.insert(tbHouseStyle);
            String[] imageUrl = houseStyleDto.getImageUrl();
            for (String s : imageUrl) {
                TbRelationRoomPicture tbRelationRoomPicture = new TbRelationRoomPicture();
                tbRelationRoomPicture.setRoomId(tbHouseStyle.getId());
                tbRelationRoomPicture.setPictureUrl(s);
                log.info(tbRelationRoomPicture.toString() + "===");
                relationRoomPictureMapper.insert(tbRelationRoomPicture);
            }


        }


        String[] imageUrl = tbHouseInfoDto.getImageUrl();
        for (String s : imageUrl) {
            TbRelationHousePicture tbRelationHousePicture = new TbRelationHousePicture();
            tbRelationHousePicture.setHouseId(tbHouseInfo.getId());
            tbRelationHousePicture.setPictureUrl(s);
            tbRelationHousePictureMapper.insert(tbRelationHousePicture);
        }
        if (insert > 0) {
            return ResultEntity.ok("增加成功");
        } else {
            return ResultEntity.error("增加失败");
        }

    }

    @Override
    public Page<TbHouseInfo> getHouse(HouseInfoDto houseInfoDto, Page<TbHouseInfo> page) {
        return tbHouseInfoMapper.getHouse(houseInfoDto, page);
    }

    @Override
    public Page<TbHouseInfo> getExtensionHouse(HouseInfoDto houseInfoDto, Page<TbHouseInfo> page) {
        return tbHouseInfoMapper.getExtensionHouse(houseInfoDto,page);
    }
}
